/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhnb
 * Date: 2011-4-11 下午01:57:40
 */

package com.ccx.dbpool;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置文件加载器
 * @author zhnb
 *
 */
public class ConfigLoader implements Serializable{
	
	private static final long serialVersionUID = 7086513269335089338L;
	private static Logger logger = LoggerFactory.getLogger(ConfigLoader.class);
	
	//配置文件
	private String confile;
	
	/**
	 * 读取默认配置文件
	 * @return			配置信息集
	 * @throws FileNotFoundException 
	 * @throws DocumentException 
	 */
	public List<ConfigInfo> getConfigInfoList() throws FileNotFoundException, DocumentException{
		//设定默认配置文件
		this.confile = "bonecp-config.xml";
		return init();
	}
	
	/**
	 * 读取指定配置文件
	 * @param confile
	 * @return			配置信息集
	 * @throws FileNotFoundException 
	 * @throws DocumentException 
	 */
	public List<ConfigInfo> getConfigInfoList( String confile ) throws FileNotFoundException, DocumentException{
		//设定配置文件
		this.confile = confile;
		return init();
	}
	
	/**
	 * 初始化池
	 * @return							配置信息集
	 * @throws FileNotFoundException 	文件不存在
	 * @throws DocumentException 
	 */
	private List<ConfigInfo> init() throws FileNotFoundException, DocumentException{
		
		//读取文件
		ClassLoader classLoader=this.getClass().getClassLoader();
		InputStream inStream = classLoader.getResourceAsStream( this.confile );
		
		//流为空，则抛出文件未找到异常。
		if ( inStream == null ) {
			throw new FileNotFoundException("cause by the file '" 
					+ this.confile + "' is not exist in resource directory." );
		}
		
		//池配置信息集
		List<ConfigInfo> bcpList = new ArrayList<ConfigInfo>(3);
		//数据源信息
		Element dbsrcInfo;
		
		//创建Dom4j解析器，解析配置文件。
		SAXReader reader = new SAXReader();
		Document doc = reader.read( inStream );
		Element root = doc.getRootElement();
		//循环数据源
		for ( Iterator<Element> iter = root.elementIterator("dataSource"); iter.hasNext(); ) {
			dbsrcInfo = iter.next();
			
			//创建数据源配置信息对象
			ConfigInfo config = new ConfigInfo();
			
			config.setDataSourceName( dbsrcInfo.elementText("dsName") );
			config.setJdbcUrl( dbsrcInfo.elementText("jdbcUrl") ); 
			config.setJdbcClass( dbsrcInfo.elementText("jdbcClass") ); 
			config.setUsername( dbsrcInfo.elementText("userName") ); 
			config.setPassword( dbsrcInfo.elementText("userPwd") );
			
			String minStr = dbsrcInfo.elementText("minConnsPerPartition");
			String maxStr = dbsrcInfo.elementText("maxConnsPerPartition");
			String countStr = dbsrcInfo.elementText("partitionCount");
			
			int min = (minStr == null || minStr.trim().length() < 1) ? 3 : Integer.parseInt( minStr );
			int max = (maxStr == null || maxStr.trim().length() < 1) ? 5 : Integer.parseInt( maxStr );
			int count = (countStr == null || countStr.trim().length() < 1) ? 1 : Integer.parseInt( countStr );
			
			config.setMinConnectionsPerPartition( min );
			config.setMaxConnectionsPerPartition( max );
			config.setPartitionCount( count );
			
			//添加到数据源配置信息集
			bcpList.add( config );
			
		}
		logger.info( "load data sources: " + (bcpList == null ? null : "" +bcpList.size() ) );
		return bcpList;
	}
}
